package com.zbcbms.service.impl;

import com.zbcbms.dao.BookBorrowDao;
import com.zbcbms.dao.UserDao;
import com.zbcbms.dao.impl.BookBorrowDaoImpl;
import com.zbcbms.dao.impl.UserDaoImpl;
import com.zbcbms.entity.User;
import com.zbcbms.entity.chart.UserJoinTimeCountChart;
import com.zbcbms.entity.chart.UserRoleCountChart;
import com.zbcbms.entity.tableData.UserTableData;
import com.zbcbms.entity.vo.UserVo;
import com.zbcbms.service.UserService;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import java.util.List;

/**
 * @author fengq
 */
public class UserServiceImpl implements UserService {

    private UserDao userDao=new UserDaoImpl();
    private BookBorrowDao bookBorrowDao=new BookBorrowDaoImpl();

    @Override
    public Boolean saveOne(User entity) {
        return userDao.insertOne(entity);
    }

    @Override
    public Boolean updateById(User entity) {
        return userDao.update(entity);
    }

    @Override
    public Boolean deleteById(String id) {
        bookBorrowDao.updateDeletedByBookNumber(id,1);
        return userDao.deleteById(id);
    }

    @Override
    public int deleteByIds(List<String> ids) {
        return userDao.deleteByIds(ids);
    }

    @Override
    public Boolean pjySicsDeleteById(String id) {
        return userDao.pjySicsDeleteById(id);
    }

    @Override
    public Boolean recoverById(String id) {
        bookBorrowDao.updateDeletedByBookNumber(id,0);
        return userDao.recoverById(id);
    }

    @Override
    public String getPasswordByNumber(String number) {
        return userDao.selectPasswordByNumber(number);
    }

    @Override
    public ObservableList<UserTableData> getAllUserTableData() {
        List<UserVo> userVos = userDao.selectAllUser();
        return conversion(userVos);
    }

    @Override
    public ObservableList<String> getAllUserNumber() {
        List<String> list = userDao.selectAllUserNumber();
        ObservableList<String> observableList=FXCollections.observableArrayList();
        for (String str:list){
            observableList.add(str);
        }
        return observableList;
    }

    @Override
    public ObservableList<String> getUserNumberByKeyword(String keyword) {
        List<String> list = userDao.selectUserNumberByKeyword(keyword);
        ObservableList<String> observableList=FXCollections.observableArrayList();
        for (String str:list){
            observableList.add(str);
        }
        return observableList;
    }

    @Override
    public String getUserNameByNumber(String number) {
        return userDao.selectUserNameByNumber(number);
    }

    @Override
    public UserVo getUserByNumber(String number) {
        return userDao.selectOneById(number);
    }

    @Override
    public List<UserRoleCountChart> getUserCountByRole() {
        return userDao.selectUserCountByRole();
    }

    @Override
    public List<UserJoinTimeCountChart> getUserCountByJoinTime() {
        return userDao.selectUserCountBtJoinTme();
    }

    @Override
    public ObservableList<UserTableData> getAllDeletedUser() {
        List<UserVo> list = userDao.selectAllDeletedUser();
        return conversion(list);
    }

    @Override
    public ObservableList<UserTableData> getAllUserVoByKeyword(String keyword) {
        List<UserVo> list = userDao.selectUserVoByKeyWord(keyword);
        return conversion(list);
    }

    public ObservableList<UserTableData> conversion(List<UserVo> list){
        ObservableList<UserTableData> observableList=FXCollections.observableArrayList();
        for (UserVo vo:list){
            observableList.add(new UserTableData(vo.getUserNumber(),vo.getUserName(),vo.getJoinDate(),vo.getRole()));
        }
        return observableList;
    }
}
